package com.health.tools;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * Android 保存List数据（SharedPreferences）
 *
 * @author nova
 *
 */
public class ListDataSave {
	private SharedPreferences preferences;
	private SharedPreferences.Editor editor;

	public ListDataSave(Context mContext, String preferenceName) {
		preferences = mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
		editor = preferences.edit();
	}

	/**
	 * 保存List
	 *
	 * @param tag
	 * @param datalist
	 */
	public <T> void setDataList(String tag, List<T> datalist) {
		if (null == datalist || datalist.size() <= 0)
			return;

		Gson gson = new Gson();
		// 转换成json数据，再保存
		String strJson = gson.toJson(datalist);
		Log.e(tag+"set()", strJson);
//		editor.clear();
		editor.putString(tag, strJson);
		editor.commit();

	}
	/**
	 * 获取List
	 *
	 * @param tag
	 * @return
	 */
	public <T> List<T> getDataList(String tag,Class<T> cls) {
		List<T> datalist = new ArrayList<T>();
		String strJson = preferences.getString(tag, null);
		Log.e(tag, strJson);
		if (null == strJson) {
			return datalist;
		}
		try {
			Gson gson = new Gson();
			JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
			for (JsonElement jsonElement : arry) {
				datalist.add(gson.fromJson(jsonElement, cls));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return datalist;

	}

}
